home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 November / november_2001.iso / Browsers / Netscape 6.1 / browser.xpi / bin / chrome / comm.jar / content / wallet / walletOverlay.js < prev    next >
Encoding:
JavaScript  |  2001-05-29  |  11.6 KB  |  314 lines

  1. /* -*- Mode: Java; tab-width: 4; c-basic-offset: 4; -*-
  2.  * 
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  *
  18.  * Contributor(s):
  19.  */
  20.  
  21.     // Set the disabled attribute of specified item.
  22.     //   If the value is false, then it removes the attribute
  23.     function setDisabledAttr(id, val) {
  24.       var elem = document.getElementById(id);
  25.       if (elem) {
  26.         if (val) {
  27.           elem.setAttribute("disabled", val);
  28.         } else {
  29.           elem.removeAttribute("disabled");
  30.         }
  31.       }
  32.     }
  33.  
  34.     // Show/hide one item (specified via name or the item element itself).
  35.     function showItem(itemOrId, show) {
  36.       var item = null;
  37.       if (itemOrId.constructor == String) {
  38.         // Argument specifies item id.
  39.         item = document.getElementById(itemOrId);
  40.       } else {
  41.         // Argument is the item itself.
  42.         item = itemOrId;
  43.       }
  44.       if (item) {
  45.         var styleIn = item.getAttribute("style");
  46.         var styleOut = styleIn;
  47.         if (show) {
  48.           // Remove style="display:none;".
  49.           styleOut = styleOut.replace("display:none;", "");
  50.  
  51.         } else {
  52.           // Set style="display:none;".
  53.           if (styleOut.indexOf("display:none;") == -1) {
  54.             // Add style the first time we need to.
  55.             styleOut += "display:none;";
  56.           }
  57.         }
  58.         // Only set style if it's different.
  59.         if (styleIn != styleOut) {
  60.           item.setAttribute("style", styleOut);
  61.         }
  62.       }
  63.     }
  64.  
  65. /* form toolbar is out
  66.     var firstTime = true;
  67.  
  68.     function initToolbarItems() {
  69.  
  70.       // This routine determines whether or not to display the form-manager toolbar and,
  71.       // if so, which buttons on the toolbar are to be enabled.  We need to reexecute
  72.       // this routine whenever the form-manager dialog finishes because saved values might
  73.       // have been added/removed which could affect the enable/disable state of the buttons.
  74.  
  75.       if (firstTime) {
  76.         // Force initToolbarItems to be executed upon return from viewing prefs.
  77.         //   This is necessary in case the form-manager dialog was invoked from the
  78.         //   pref panel.  See next block of code for more details.
  79.         var pref = document.getElementById("menu_preferences");
  80.         if (pref) {
  81.           oncommand = pref.getAttribute("oncommand");
  82.           pref.setAttribute("oncommand", oncommand+";initToolbarItems()");
  83.           firstTime = false;
  84.         }
  85.       }
  86.  
  87.       // get the form-manager toolbar
  88.       var cmd_viewformToolbar = document.getElementById("cmd_viewformtoolbar");
  89.       if (!cmd_viewformToolbar) {
  90.         // This happens when you access the form-manager dialog from the prefs panel
  91.         // Not sure yet how to get access to items in navigator in that case
  92.         // So instead we will execute initToolbarItems when edit->prefs returns (that's
  93.         // what above block of code involving firstTime accomplished.
  94.         return;
  95.       }
  96.  
  97.       // keep form toolbar hidden if checkbox in view menu so indicates
  98.       var checkValue = cmd_viewformToolbar.getAttribute("checked");
  99.       if (checkValue == "false") {
  100.         showItem("formToolbar", false);
  101.         return;
  102.       }
  103.  
  104.       // hide form toolbar if three or less text elements in form
  105.       var prefillState = getState(prefill, 3);
  106.       showItem("formToolbar", (prefillState != hide));
  107.  
  108.       // enable prefill button if there is at least one saved value for the form
  109.       setDisabledAttr("formPrefill", (prefillState == disable));
  110.     }
  111. */
  112.  
  113.     function formShow() {
  114.       window.openDialog(
  115.           "chrome://communicator/content/wallet/WalletViewer.xul",
  116.           "WalletViewer",
  117.           "chrome,titlebar,modal=yes,resizable=yes");
  118. /* form toolbar is out
  119.        initToolbarItems(); // need to redetermine which buttons in form toolbar to enable
  120. */
  121.     }
  122.  
  123.     // Capture the values that are filled in on the form being displayed.
  124.     function formCapture() {
  125.       var walletService = Components.classes["@mozilla.org/wallet/wallet-service;1"].getService(Components.interfaces.nsIWalletService);
  126.       walletService.WALLET_RequestToCapture(window._content);
  127.     }
  128.  
  129.     // Prefill the form being displayed.
  130.     function formPrefill() {
  131.       var walletService = Components.classes["@mozilla.org/wallet/wallet-service;1"].getService(Components.interfaces.nsIWalletService);
  132.       walletService.WALLET_Prefill(false, window._content);
  133.       window.openDialog("chrome://communicator/content/wallet/WalletPreview.xul",
  134.                         "_blank", "chrome,modal=yes,dialog=yes,all, width=504, height=436");
  135.     }
  136.  
  137. /*
  138.     // Prefill the form being displayed without bringing up the preview window.
  139.     function formQuickPrefill() {
  140.       var walletService = Components.classes["@mozilla.org/wallet/wallet-service;1"].getService(Components.interfaces.nsIWalletService);
  141.       walletService.WALLET_Prefill(true, window._content);
  142.     }
  143. */
  144.  
  145.     var hide = -1;
  146.     var disable = 0;
  147.     var enable = 1;
  148.  
  149.     var capture = 0;
  150.     var prefill = 1;
  151.  
  152.     var elementCount;
  153.  
  154.     // Walk through the DOM to determine how a capture or prefill item is to appear.
  155.     //   arguments:
  156.     //      captureOrPrefill = capture, prefill
  157.     //   returned value:
  158.     //      hide, disable, enable
  159.     function getStateFromFormsArray(formsArray, captureOrPrefill, threshhold, walletService) {
  160.       if (!formsArray) {
  161.         return hide;
  162.       }
  163.  
  164.       var form;
  165.       var bestState = hide;
  166.  
  167.       for (form=0; form<formsArray.length; form++) {
  168.         var elementsArray = formsArray[form].elements;
  169.         var element;
  170.         for (element=0; element<elementsArray.length; element++) {
  171.           var type = elementsArray[element].type;
  172.           if ((type=="") || (type=="text") || (type=="select-one")) {
  173.             // we have a form with at least one text or select element
  174.             if (type != "select-one") {
  175.               elementCount++;
  176.             }
  177.  
  178.             /* If database is encrypted and user has not yet supplied master password,
  179.              * we won't be able to access the data.  In that case, enable the item rather
  180.              * than asking user for password at this time.  Otherwise you'll be asking for
  181.              * the password whenever user clicks on edit menu or context menu
  182.              */
  183.             try {
  184.               if (this.pref.GetBoolPref("wallet.crypto")) {
  185.                 // database is encrypted, see if it is still locked
  186. //              if (locked) { -- there's currently no way to make such a test
  187.                   // it's encrypted and locked, we lose
  188.                   return enable;
  189. //              }
  190.               }
  191.             } catch(e) {
  192.               // there is no crypto pref so database could not possible be encrypted
  193.             }
  194.  
  195.             if (bestState == hide) {
  196.               bestState = disable;
  197.             }
  198.             var value;
  199.  
  200.             // obtain saved values if any and store in array called valueList
  201.             var valueList;
  202.             var valueSequence = walletService.WALLET_PrefillOneElement
  203.               (window._content, elementsArray[element]);
  204.             // result is a linear sequence of values, each preceded by a separator character
  205.             // convert linear sequence of values into an array of values
  206.             if (valueSequence) {
  207.               var separator = valueSequence[0];
  208.               valueList = valueSequence.substring(1, valueSequence.length).split(separator);
  209.             }
  210.  
  211.             // see if there's a value on screen (capture case) or a saved value (prefill case) 
  212.             if (captureOrPrefill == capture) {
  213.               // in capture case, see if element has a value on the screen which is not saved
  214.               value = elementsArray[element].value;
  215.               if (valueSequence && value) {
  216.                 for (var i=0; i<valueList.length; i++) {
  217.                   if (value == valueList[i]) {
  218.                     value = null;
  219.                     break;
  220.                   }
  221.                 }
  222.               }
  223.             } else {
  224.               // in prefill case, see if element has a saved value
  225.               if (valueSequence) {
  226.                 value = valueList[0];
  227.               }
  228.             }
  229.  
  230.             if (value) {
  231.               // at least one text (or select) element has a value,
  232.               //    in which case the capture or prefill item is to appear in menu
  233.               bestState = enable;
  234.               if (elementCount > threshhold) {
  235.                 return enable;
  236.               }
  237.             }
  238.           } 
  239.         }
  240.       }
  241.       // if we got here, then there was no element with a value or too few elements
  242.       return bestState;
  243.     }
  244.  
  245.     // Walk through the DOM to determine how capture or prefill item is to appear.
  246.     //   arguments:
  247.     //      captureOrPrefill = capture, prefill
  248.     //   returned value:
  249.     //      hide, disable, enable
  250.     function getState(captureOrPrefill, threshhold) {
  251.       if (!window._content || !window._content.document) {
  252.         return hide;
  253.       }
  254.       var document = window._content.document;
  255.       if (!("forms" in document)) {
  256.         // this will occur if document is xul document instead of html document for example
  257.         return hide;
  258.       }
  259.  
  260.       // test for wallet service being available
  261.       var walletService = Components.classes["@mozilla.org/wallet/wallet-service;1"]
  262.         .getService(Components.interfaces.nsIWalletService);
  263.       if (!walletService) {
  264.         return hide;
  265.       }
  266.  
  267.       var bestState = hide;
  268.       var state;
  269.       elementCount = 0;
  270.  
  271.       // process frames if any
  272.       var formsArray;
  273.       var framesArray = window._content.frames;
  274.       if (framesArray.length != 0) {
  275.         var frame;
  276.         for (frame=0; frame<framesArray.length; frame++) {
  277.           formsArray = framesArray[frame].document.forms;
  278.           state =
  279.             getStateFromFormsArray(formsArray, captureOrPrefill, threshhold, walletService);
  280.           if (state == enable) {
  281.             if (elementCount > threshhold) {
  282.               return enable;
  283.             }
  284.             bestState = enable;
  285.           } else if (state == disable && bestState == hide) {
  286.             bestState = disable;
  287.           }
  288.         }
  289.       }
  290.  
  291.       // process top-level document
  292.       formsArray = document.forms;
  293.       state =
  294.         getStateFromFormsArray(formsArray, captureOrPrefill, threshhold, walletService);
  295.       if (state == enable) {
  296.         if (elementCount > threshhold) {
  297.           return enable;
  298.         }
  299.         bestState = enable;
  300.       } else if (state == disable && bestState == hide) {
  301.         bestState = disable;
  302.       }
  303.  
  304.       // if we got here, then there was no text (or select) element with a value
  305.       // or there were too few text (or select) elements
  306.       if (elementCount > threshhold) {
  307.         // no text (or select) element with a value
  308.         return bestState;
  309.       }
  310.  
  311.       // too few text (or select) elements
  312.       return hide;
  313.     }
  314.